//
//  ViewController.m
//  获取手机截屏的图片
//
//  Created by lemo on 2017/11/1.
//  Copyright © 2017年 lemo. All rights reserved.
//

#import "ViewController.h"
#import "ScreenshotImage.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *screenshotImage;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(userDidTaskScreenshot:) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
}

- (void)userDidTaskScreenshot:(NSNotification *)notification {
    NSLog(@"检测到截屏");
    
    //人为截屏, 模拟用户截屏行为, 获取所截图片
    UIImage *image_ = [[ScreenshotImage sharedInstance] imageWithScreenshot];
    
    self.screenshotImage.image = image_;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


//- (UIImage *)imageWithScreenshot {
//    NSData *imageData = [self dataWithScreenshotInPNGFormat];
//    return [UIImage imageWithData:imageData];
//}
//
//- (NSData *)dataWithScreenshotInPNGFormat {
//    CGSize imageSize = CGSizeZero;
//    UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
//    if (UIInterfaceOrientationIsPortrait(orientation))
//        imageSize = [UIScreen mainScreen].bounds.size;
//    else
//        imageSize = CGSizeMake([UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
//    
//    UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    for (UIWindow *window in [[UIApplication sharedApplication] windows])
//    {
//        CGContextSaveGState(context);
//        CGContextTranslateCTM(context, window.center.x, window.center.y);
//        CGContextConcatCTM(context, window.transform);
//        CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
//        if (orientation == UIInterfaceOrientationLandscapeLeft)
//        {
//            CGContextRotateCTM(context, M_PI_2);
//            CGContextTranslateCTM(context, 0, -imageSize.width);
//        }
//        else if (orientation == UIInterfaceOrientationLandscapeRight)
//        {
//            CGContextRotateCTM(context, -M_PI_2);
//            CGContextTranslateCTM(context, -imageSize.height, 0);
//        } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
//            CGContextRotateCTM(context, M_PI);
//            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
//        }
//        if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)])
//        {
//            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
//        }
//        else
//        {
//            [window.layer renderInContext:context];
//        }
//        CGContextRestoreGState(context);
//    }
//    
//    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//    UIGraphicsEndImageContext();
//    
//    return UIImagePNGRepresentation(image);
//}

@end
